package com.yykj.im.center.server.util;

import com.yykj.im.center.client.dto.UserInfoDto;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class ConvertUtil {

    public static <T> List<T> convertList(List list, Class<T> clazz) {
        if (list == null || list.size() == 0) {
            return null;
        }
        List<T> target = new ArrayList<>();
        for (Object o : list) {
            T convert = convert(o, clazz);
            if (convert != null) {
                target.add(convert);
            }
        }
        return target;
    }

    public static <T> T convert(Object source, Class<T> clazz) {
        try {
            T t = clazz.newInstance();
            BeanUtils.copyProperties(source, t);
            return t;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static ArrayList<UserInfoDto> getUserInfo(String str){
        String[] s = str.split("@@@");
        ArrayList<UserInfoDto> idNameDtos = new ArrayList<>();
        if(s.length > 0){
            for(String z : s){
                String[] user = z.split(",");
                if(user.length == 2){
                    idNameDtos.add(UserInfoDto.builder()
                            .tencentAccountId(user[0])
                            .name(user[1]).build());
                }
            }
        }
        return idNameDtos;
    }

    public static void main(String[] args) {
        String a = "prod2b6aa22670524c40a2b899dafa8c0842,南山南";
        System.out.println(getUserInfo(a));
    }

}